﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;

namespace CRM.Web.MessagingManage
{
    public partial class SMSSentList : Common.PageBase
    {

        public string ModifyUrl = "SMSShow.aspx?AUTOID={0}&SMSType=SENT";
        T_SMS_SENT_BLL bll = new T_SMS_SENT_BLL();
        //SqlServerDAL<T_SMS_SENT_Entity> dal = new SqlServerDAL<T_SMS_SENT_Entity>();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlRFLAGBinding();
                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定

        private void ddlRFLAGBinding()
        {
            ddlRFLAG.Items.Clear();
            EnumHelper.FillListControl(ddlRFLAG, typeof(SMSStatus));
            ddlRFLAG.Items.Insert(0, new ListItem("", ""));
        }



        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["AUTOID"]))
            {
                sql.Append(" and AUTOID like '%" + Request.QueryString["AUTOID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CALLID"]))
            {
                sql.Append(" and CALLID = '" + Request.QueryString["CALLID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["TEL_TO"]))
            {
                sql.Append(" and TEL_TO like '%" + Request.QueryString["TEL_TO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["UNAME"]))
            {
                sql.Append(" and UNAME like '%" + Request.QueryString["UNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SUBJECTS"]))
            {
                sql.Append(" and SUBJECTS like '%" + Request.QueryString["SUBJECTS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BODYS"]))
            {
                sql.Append(" and BODYS like '%" + Request.QueryString["BODYS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LEVELS"]))
            {
                sql.Append(" and LEVELS like '%" + Request.QueryString["LEVELS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SDATE"]))
            {
                sql.Append(" and SDATE like '%" + Request.QueryString["SDATE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STIME"]))
            {
                sql.Append(" and STIME like '%" + Request.QueryString["STIME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RETRY"]))
            {
                sql.Append(" and RETRY like '%" + Request.QueryString["RETRY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["INTERVL"]))
            {
                sql.Append(" and INTERVL like '%" + Request.QueryString["INTERVL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GHID"]))
            {
                sql.Append(" and GHID = '" + Request.QueryString["GHID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["UINFO"]))
            {
                sql.Append(" and UINFO like '%" + Request.QueryString["UINFO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RFLAG"]))
            {
                sql.Append(" and RFLAG = '" + Request.QueryString["RFLAG"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            }

            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }


            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
                switch (UserLoginInfo.GetCurrentUserType())
                {
                    case UserType.中心部门:
                        {

                        }
                        break;
                    case UserType.市场部:
                    case UserType.供应商:
                    case UserType.服务商:
                    case UserType.经销商:
                        {
                            sql.Append(String.Format(" and GHID = '{0}' ", _userLoginInfo.UserEntity.USERID.ToString()));
                        }
                        break;


                }
            }
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_SMS_SENT_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "AUTOID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_SMS_SENT_Entity entity = new T_SMS_SENT_Entity();
            entity.AUTOID = StringHelper.ToInt((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "TEL_TO":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            temp = (temp.Length >= 20) ? temp.Substring(0, 20) + "..." : temp;
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    case "BODYS":
                                        {
                                            temp = e.Row.Cells[i].Text.Trim();
                                            temp = (temp.Length >= 30) ? temp.Substring(0, 30) + "..." : temp;
                                            e.Row.Cells[i].Text = temp;
                                        }
                                        break;
                                    case "RFLAG":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = ((SMSStatus)Enum.Parse(typeof(SMSStatus), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                                
                                            }
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
